package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /***
     * shoppingCartDTO：菜品id 套餐id 菜品的口味
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //创建对象，把值赋值给新对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //根据条件判断对象在不在数据库中存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //进行判断
        if (list != null && list.size() > 0){
            //如果有数据的话就把数量+1
            //因为我们根据条件查询的是固定的一个对象，我们采用list是为了查看购物车时增强复用性
            ShoppingCart shopping = list.get(0);
            //获取到当前购物车数据后，给他原本的数量+1
            Integer number = shopping.getNumber();
            shopping.setNumber(number + 1);
            //进行修改数据库文件
            shoppingCartMapper.updateNumber(shopping);
        }else {
            //如果没有数据的话，就把shoppingCart剩余数据补齐插入进数据库
            //首先使用前端给我们传的值进行判断新增的是套餐还是菜品
            if (shoppingCartDTO.getDishId() != null){
                //数据不为null，说明是菜品，我们需要获取菜品的全部属性，进行添加
                Dish dish = dishMapper.selectByIdDish(shoppingCartDTO.getDishId().intValue());
                //进行添加
                //根据表的字段可以得知Number默认为1，不需要赋值
                shoppingCart.setNumber(1);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                //因为数据里原本就有口味信息，添加没有的信息即可
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //进入这里说明是套餐，我们需要获取套餐的全部属性，进行添加
                //套餐和菜品同理
                SetmealDTO setmealDTO = setmealMapper.selectByIdSetmeal(shoppingCartDTO.getSetmealId().intValue());
                //根据表的字段可以得知Number默认为1，不需要赋值
                shoppingCart.setNumber(1);
                shoppingCart.setName(setmealDTO.getName());
                shoppingCart.setImage(setmealDTO.getImage());
                shoppingCart.setAmount(setmealDTO.getPrice());
            }

            //这里是菜品或套餐的共同添加字段
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            //进行插入数据库
            shoppingCartMapper.insert(shoppingCart);
        }

    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> selectShoppingCart() {
        //查询全部数据，直接调用mapper层
        //根据当前用户id去查询购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteShoppingCart() {
        //我们需要根据用户id去清楚购物车
        //查询当前用户id，删除这个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteShoppingCart(currentId);
    }

    /**
     * 删除单个数据
     * shoppingCartDTO：菜品id 套餐id 口味
     * @param shoppingCartDTO
     */
    @Override
    public void deletesingle(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //获取按要求查询到的数据
        ShoppingCart shopping = list.get(0);
        Integer number = shopping.getNumber();
        //如果数量大于1就减一
        if (number>1) {
            shopping.setNumber(number -1);
            shoppingCartMapper.updateNumber(shopping);
        }else {
            //最后一次删除直接删除这个数据
            Long id = shopping.getId();
            shoppingCartMapper.delete(id);
        }
    }
}
